import React from 'react'
import * as xlsx from 'xlsx'

const JsonToExcel = () => {
    let json;
    const handleChange = (e) => {
        console.log(e.target.files[0])
        let uploadFile = e.target.files[0]
        let reader = new FileReader()
        reader.readAsText(uploadFile, "GBK")
        reader.onload = function () {
            json = JSON.parse(this.result);
        }
    }
    const handleClick = () => {
        let filename = "new-doc.xlsx"
        let ws_name = "SheetJS"
        let wb = xlsx.utils.book_new()
        let ws = xlsx.utils.json_to_sheet(json)
        xlsx.utils.book_append_sheet(wb, ws, ws_name)
        xlsx.writeFile(wb, filename)
    }
    return <div className='show-wrap'>
        <input
            type="file"
            id='files'
            onChange={handleChange}
        />
        <button onClick={handleClick}>导出为excel</button>
    </div>
}

export default JsonToExcel